﻿using IP20.Procdure;
using IP20.ViewModel;
using OfficeOpenXml.FormulaParsing.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace IP20.View
{
    /// <summary>
    /// DebugView.xaml 的交互逻辑
    /// </summary>
    public partial class DebugView : UserControl
    {
        public DebugView()
        {
            InitializeComponent();
            this.DataContext = new DebugViewModel();
            CreateHueGrid();
            this.TiJq89313Config.Visibility = Global.IsJq8931314;
            this.TiJq89314Config.Visibility = Global.IsJq8931314;
        }

        private void CreateHueGrid()
        {
            UniformGrid grid = this.ufColor;
            if (grid == null) return;

            for (int i = 0; i < 360; i++)
            {
                // 计算当前色相（HSV: H=i°, S=100%, V=100%）
                Color color = ColorFromHSV(i, 1.0, 1.0);
                Border border = new Border
                {
                    Background = new SolidColorBrush(color),
                    ToolTip = $"色相: {i}°",
                    Margin = new Thickness(1)
                };
                grid.Children.Add(border);
            }
        }

        // HSV转RGB方法
        private Color ColorFromHSV(double hue, double saturation, double value)
        {
            int hi = (int)(hue / 60) % 6;
            double f = hue / 60 - (int)(hue / 60);
            double p = value * (1 - saturation);
            double q = value * (1 - f * saturation);
            double t = value * (1 - (1 - f) * saturation);

            System.Drawing.Color color;
            switch (hi)
            {
                case 0: color = System.Drawing.Color.FromArgb((int)(value * 255), (int)(t * 255), (int)(p * 255)); break;
                case 1: color = System.Drawing.Color.FromArgb((int)(q * 255), (int)(value * 255), (int)(p * 255)); break;
                case 2: color = System.Drawing.Color.FromArgb((int)(p * 255), (int)(value * 255), (int)(t * 255)); break;
                case 3: color = System.Drawing.Color.FromArgb((int)(p * 255), (int)(q * 255), (int)(value * 255)); break;
                case 4: color = System.Drawing.Color.FromArgb((int)(t * 255), (int)(p * 255), (int)(value * 255)); break;
                default: color = System.Drawing.Color.FromArgb((int)(value * 255), (int)(p * 255), (int)(q * 255)); break;
            }
            return Color.FromArgb(color.A, color.R, color.G, color.B);
        }
    }
}